package com.sengled.cloud.biz.union.mgt.api.controller;

import com.alibaba.fastjson.JSON;
import com.sengled.cloud.biz.union.mgt.api.config.SengledProperties;
import com.sengled.cloud.biz.union.mgt.common.util.CurrentUserUtil;
import com.sengled.cloud.biz.union.mgt.model.request.ChangeCurrentShopRequest;
import com.sengled.cloud.biz.union.mgt.model.request.ChangeUserPwdRequest;
import com.sengled.cloud.biz.union.mgt.model.request.FeedBackRequest;
import com.sengled.cloud.biz.union.mgt.model.request.GetCurrentShopsRequest;
import com.sengled.cloud.biz.union.mgt.model.response.ResponseMessage;
import com.sengled.cloud.biz.union.mgt.model.vo.CurrentShopsVo;
import com.sengled.cloud.biz.union.mgt.model.vo.GetCurrentShopVo;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopAccountListVo;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopAccountVo;
import com.sengled.cloud.biz.union.mgt.service.sys.IRoleManageService;
import com.sengled.cloud.biz.union.mgt.service.sys.IUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by csolo on 2017/11/23.
 */

@RestController
@RequestMapping("/api/app/v1")
public class AppController {

    @Autowired
    private IUserService userService;
    @Autowired
    private SengledProperties sengledProperties;

    @ApiOperation(value = "测试获取用户所属店铺列表接口", notes = "获取用户所属店铺列表接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    @RequestMapping(value = "/user/shop/", method = RequestMethod.GET)
    public ResponseMessage<ShopAccountListVo> getUserShops(HttpServletRequest request) {
        //通过Header中的sessionId，从redis中取出用户信息
        Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        ResponseMessage<ShopAccountListVo> rm = userService.getUserShops(customerId);
        return rm;
    }

    @ApiOperation(value = "测试用户切换店铺接口", notes = "获取用户切换店铺接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    @RequestMapping(value = "/user/stay/shop/", method = RequestMethod.PUT)
    public ResponseMessage<CurrentShopsVo> changeUserCurrentShop(@RequestBody ChangeCurrentShopRequest changeCurrentShopRequest, HttpServletRequest request) {
        //通过Header中的sessionId，从redis中取出用户信息
        Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        ResponseMessage<CurrentShopsVo> rm = userService.changeShop(changeCurrentShopRequest.getId(),customerId,changeCurrentShopRequest.getModuleCode());
        return rm;
    }

    @ApiOperation(value = "测试修改用户密码接口", notes = "修改用户密码接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    @RequestMapping(value = "/user/password/", method = RequestMethod.PATCH)
    public ResponseMessage changeUserPwd(@RequestBody ChangeUserPwdRequest changeUserPwdRequest, HttpServletRequest request) {
        //通过Header中的sessionId，从redis中取出用户信息
        Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        ResponseMessage rm = userService.changePwd(customerId,changeUserPwdRequest.getOldPassword(),changeUserPwdRequest.getNewPassword());
        return rm;
    }

    @ApiOperation(value = "测试用户提交反馈接口", notes = "用户提交反馈接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    @RequestMapping(value = "/user/feedback/", method = RequestMethod.POST)
    public ResponseMessage saveUserFeedback(@ApiParam(name = "feedback",value = "反馈内容", required = true) @RequestBody FeedBackRequest feedBackRequest, HttpServletRequest request) {
        //通过Header中的sessionId，从redis中取出用户信息
        ResponseMessage rm = new ResponseMessage();
        Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        String feedBackContent = feedBackRequest.getFeedback();
        if(StringUtils.isEmpty(feedBackContent)){
            rm.buildParamErrorMessage();
        }else{
            rm = userService.saveFeedback(feedBackRequest.getFeedback(),customerId);
        }
        return rm;
    }

    @ApiOperation(value = "测试上传用户头像接口", notes = "上传用户头像接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    @RequestMapping(value = "/user/profile/upload/", method = RequestMethod.POST)
    public ResponseMessage uploadUserProfile(@ApiParam(value = "头像文件", required = true) MultipartFile profilePhoto, HttpServletRequest request) {
        ResponseMessage rm = new ResponseMessage();
        long fileSize = profilePhoto.getSize();
        if (fileSize <= 0 || fileSize > (1024 * 1024)) {
            rm.buildUploadFileFailMessage();
            return rm;
        }
        String fileName = profilePhoto.getOriginalFilename();
        // 获取上传文件扩展名
        String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
        // 对扩展名进行小写转换
        fileExt = fileExt.toLowerCase();
        // 图片文件格式
        if (!"jpg".equals(fileExt) && !"jpeg".equals(fileExt) && !"png".equals(fileExt) && !"bmp".equals(fileExt)) {
            rm.buildUploadFileFailMessage();
            return rm;
        }
        Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        String region = sengledProperties.getProperty("aws_s3_region");
        String bucketName = sengledProperties.getProperty("aws_biz_picture_bucket");
        String tempPath = sengledProperties.getProperty("biz_temp_file_path");
        rm = userService.uploadUserProfile(profilePhoto,request,region,bucketName,customerId,tempPath);
        return rm;
    }

    @ApiOperation(value = "测试获取当前用户信息接口", notes = "获取当前用户信息接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    @RequestMapping(value = "/user/", method = RequestMethod.GET)
    public ResponseMessage getUser(HttpServletRequest request) {
        Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        String storagePublicAddr = sengledProperties.getProperty("AMAZON_STORAGE_PUBLIC_ADDR");
        String storagePublicPort = sengledProperties.getProperty("AMAZON_STORAGE_PUBLIC_PORT_SSL");
        String bucketName = sengledProperties.getProperty("aws_biz_picture_bucket");
        ResponseMessage rm = userService.getCurrentUser(customerId,storagePublicAddr,storagePublicPort,bucketName);
        return rm;
    }


    @ApiOperation(value = "测试获取当前店铺接口", notes = "获取当前店铺接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    @RequestMapping(value = "/user/stay/shop/", method = RequestMethod.POST)
    public String getCurrentShop(HttpServletRequest request,@ApiParam(name = "channel", required = true) @RequestBody GetCurrentShopsRequest getCurrentShopsRequest) {
        Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        ResponseMessage<GetCurrentShopVo> rm = userService.getCurrentShop(getCurrentShopsRequest.getChannel(),customerId);
        return JSON.toJSONString(rm);
    }
}
